package com.yilee.idouban.xml.service;

import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import com.yilee.idouban.bean.Person;
import com.yilee.idouban.xml.handler.SAXPersonContentHandler;

/**
 * SAX方式解析xml
 * 
 * @author yilee
 * 
 */
public class SAXPraserService {

	/**
	 * 解析授权用户的XML 形式如 http://api.douban.com/people/yilee
	 * 
	 * @param is
	 * @return Person对象
	 */
	public static Person getPersonFromXML(InputStream is) throws Exception {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser saxParser = spf.newSAXParser(); // 创建解析器
		SAXPersonContentHandler handler = new SAXPersonContentHandler();
		saxParser.parse(is, handler);
		is.close();
		return handler.getPerson();
	}

}
